# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request

import sys
sys.path.append("..")
from tutorial.items import HouseItem


class HouseSpider(scrapy.Spider):
    name = 'house'
    allowed_domains = ['house.leju.com']
    start_urls = ['http://house.leju.com/sy/search/']

    # url = "http://house.leju.com/sy/search/"

    def parse(self, response):
        # items = []
        for sel in response.xpath('//div[@class="b_card"]'):
            item = HouseItem()
            item['name'] = sel.xpath(
                'div[@class="b_titBox"]/h2/a/text()').extract()
            item['is_new'] = sel.xpath(
                'div[@class="b_titBox"]/h2/span/text()').extract()
            ii = 0
            for sel2 in sel.xpath('div[@class="b_titBox"]/h3/text()'):
                ii = ii + 1
                if ii % 2 == 1:
                    item['addr'] = sel2.extract()
                else:
                    item['Developer'] = sel2.extract()
            item['price'] = sel.xpath(
                'div[@class="b_infoBox"]/h2/strong/text()').extract()
            item['img_url'] = sel.xpath(
                'div[@class="b_imgBox"]/a/img/@src').extract()
            item['url'] = sel.xpath('div[@class="b_imgBox"]/a/@href').extract()
            # items.append(item)
            if item['name']:
                print(item['name'])
                print(item['addr'])
                yield item
                # with open('house', 'a') as f:
                #     f.writelines(str(item['name'][0]) + '   ')
                #     f.writelines(str(item['addr']) + '\n')

        nextlink = response.xpath('//a[@class="next"]/@href').extract()
        if nextlink:
            link = nextlink[0]
            print("##############")
            print(link)
            print("##############")
            yield Request(link, callback=self.parse)
        # return items
